﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    
                    xmlns:localiz="clr-namespace:ColorPickerLib.Localized"
                    xmlns:controls="clr-namespace:ColorPickerLib.Controls"
                    xmlns:local="clr-namespace:ColorPickerLib"
                    xmlns:conv="clr-namespace:ColorPickerLib.Converters"
                    xmlns:chrome="clr-namespace:ColorPickerLib.Chromes"
                    xmlns:themes="clr-namespace:ColorPickerLib.Themes"
                    >
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/ColorPickerLib;component/Themes/Generic/Buttons.xaml" />
        <ResourceDictionary Source="/ColorPickerLib;component/Chromes/Themes/Generic.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <!-- =============================================================================== -->
    <!-- ColorPicker                                                                     -->
    <!-- =============================================================================== -->

    <conv:InverseBoolConverter x:Key="InverseBoolConverter" />
    <conv:ColorToSolidColorBrushConverter x:Key="ColorToSolidColorBrushConverter" />
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

    <!-- Color Picker drop down button that opens the color picker when clicked -->
    <Style x:Key="ColorPickerToggleButtonStyle" TargetType="ToggleButton">
        <Setter Property="Background" Value="White" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ToggleButton">
                    <Grid SnapsToDevicePixels="True">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>

                            <Border Background="{TemplateBinding Background}"
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    Padding="{TemplateBinding Padding}"
                                    SnapsToDevicePixels="True">
                                <ContentPresenter Content="{TemplateBinding Content}"
                                                  ContentTemplate="{TemplateBinding ContentTemplate}"
                                                  ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
                                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
                            </Border>

                            <chrome:ButtonChrome x:Name="ToggleButtonChrome"
                                                  Grid.Column="1"
                                                  CornerRadius="{DynamicResource {x:Static themes:ResourceKeys.SpinButtonCornerRadiusKey}}"
                                                  Visibility="{Binding ShowDropDownButton, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=controls:ColorPicker}, Converter={StaticResource BooleanToVisibilityConverter}}"
                                                  BorderThickness="0,1,1,1"
                                                  RenderChecked="{Binding IsOpen, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=controls:ColorPicker}}"
                                                  RenderEnabled="{Binding IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=controls:ColorPicker}}"
                                                  RenderMouseOver="{TemplateBinding IsMouseOver}"
                                                  RenderPressed="{TemplateBinding IsPressed}">

                                <Grid x:Name="arrowGlyph" IsHitTestVisible="False" Grid.Column="1" Margin="5">
                                    <Path x:Name="Arrow" Width="7" Height="4" Data="M 0,1 C0,1 0,0 0,0 0,0 3,0 3,0 3,0 3,1 3,1 3,1 4,1 4,1 4,1 4,0 4,0 4,0 7,0 7,0 7,0 7,1 7,1 7,1 6,1 6,1 6,1 6,2 6,2 6,2 5,2 5,2 5,2 5,3 5,3 5,3 4,3 4,3 4,3 4,4 4,4 4,4 3,4 3,4 3,4 3,3 3,3 3,3 2,3 2,3 2,3 2,2 2,2 2,2 1,2 1,2 1,2 1,1 1,1 1,1 0,1 0,1 z" Fill="#FF000000" />
                                </Grid>
                            </chrome:ButtonChrome>
                        </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Fill" TargetName="Arrow" Value="#AFAFAF" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="ColorDisplayStyle" TargetType="ContentControl">
        <Setter Property="Focusable"
              Value="False" />
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Border Background="{DynamicResource CheckerBrush}">
                        <Rectangle Fill="{Binding SelectedColor, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=controls:ColorPicker}, Converter={StaticResource ColorToSolidColorBrushConverter}}" />
                    </Border>
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <DataTrigger Binding="{Binding SelectedColor, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=controls:ColorPicker}}"
                      Value="{x:Null}">
                <Setter Property="Visibility"
                    Value="Collapsed" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

    <Style x:Key="ColorItemContainerStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="ToolTip" Value="{Binding Name}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Grid ToolTip="{Binding Name}">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                        <Border BorderThickness="1" Background="Transparent" BorderBrush="Transparent" x:Name="_outerBorder" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                            <Border Background="Transparent" BorderThickness="1" BorderBrush="Transparent" x:Name="_innerBorder" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="_outerBorder" Property="BorderBrush" Value="#FFFF0000" />
                            <Setter TargetName="_innerBorder" Property="BorderBrush" Value="#FFFFFF00" />
                        </Trigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="_outerBorder" Property="BorderBrush" Value="#FFFF0000" />
                            <Setter TargetName="_innerBorder" Property="BorderBrush" Value="#FFFFFF00" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
    <DataTemplate x:Key="ColorItemTemplate">
        <Grid>
            <Border Background="{DynamicResource CheckerBrush}" BorderBrush="Black" BorderThickness="1" Margin="2,2,2,2" >
                <Rectangle Width="14" Height="14">
                    <Rectangle.Style>
                        <Style TargetType="Rectangle">
                            <Setter Property="Fill" Value="{Binding Color, Converter={StaticResource ColorToSolidColorBrushConverter}}" />
                        </Style>
                    </Rectangle.Style>
                </Rectangle>
            </Border>
        </Grid>
    </DataTemplate>

    <Style x:Key="ColorListStyle" TargetType="ListBox">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <WrapPanel Width="200" />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemContainerStyle" Value="{StaticResource ColorItemContainerStyle}" />
        <Setter Property="ItemTemplate" Value="{StaticResource ColorItemTemplate}" />
        <Setter Property="SelectionMode" Value="Single" />
    </Style>

    <Style TargetType="{x:Type controls:ColorPicker}">
        <Setter Property="Background" Value="{DynamicResource {x:Static themes:ResourceKeys.ButtonNormalBackgroundKey}}" />
        <Setter Property="BorderBrush" Value="{DynamicResource ColorPickerDarkBorderBrush}" />
        <Setter Property="BorderThickness" Value="1,1,0,1" />
        <Setter Property="ButtonStyle" Value="{StaticResource ColorPickerToggleButtonStyle}" />
        <Setter Property="Focusable" Value="False" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type controls:ColorPicker}">
                    <Grid>
                        <ToggleButton x:Name="PART_ColorPickerToggleButton"
                                    IsTabStop="True"
                                    MinHeight="22" 
                                    Background="{TemplateBinding Background}" 
                                    BorderBrush="{TemplateBinding BorderBrush}" 
                                    BorderThickness="{TemplateBinding BorderThickness}" 
                                    VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                    HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    Padding="{TemplateBinding Padding}"
                                    IsChecked="{Binding IsOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                                    IsHitTestVisible="{Binding IsOpen, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource InverseBoolConverter}}"
                                    Style="{TemplateBinding ButtonStyle}">
                            <Grid Margin="3">
                                <ContentControl x:Name="ColorOnly" Style="{StaticResource ColorDisplayStyle}" />

                                <Border x:Name="ColorAndName" Background="White" Visibility="Hidden">
                                    <StackPanel Orientation="Horizontal">
                                        <ContentControl HorizontalAlignment="Left"
                                                        Width="20" Margin="2,1,4,1"
                                                        Style="{StaticResource ColorDisplayStyle}"
                                                        BorderThickness="1" BorderBrush="#FFC9CACA" />
                                        <TextBlock Text="{Binding SelectedColorText, RelativeSource={RelativeSource TemplatedParent}}"
                                                   VerticalAlignment="Center" />
                                        
                                        <!-- Measuring textblock ensures minimal size -->
                                        <TextBlock Text="XXXXXX" VerticalAlignment="Center" Visibility="Hidden" />
                                    </StackPanel>
                                </Border>
                            </Grid>
                        </ToggleButton>

                        <Popup x:Name="PART_ColorPickerPalettePopup"
                               VerticalAlignment="Bottom"
                               IsOpen="{Binding ElementName=PART_ColorPickerToggleButton, Path=IsChecked}"
                               StaysOpen="False"
                               AllowsTransparency="True"
                               Focusable="False"
                               HorizontalOffset="0" VerticalOffset="3" PopupAnimation="None"
                               >
                            <Border BorderThickness="1"
                                    Background="{DynamicResource PopupBackgroundBrush}"
                                    BorderBrush="{DynamicResource ColorPickerDarkBorderBrush}" Padding="3">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition />
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>

                                    <Grid x:Name="_colorPaletteHost" Visibility="Collapsed" Margin="4">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="Auto" />
                                        </Grid.RowDefinitions>

                                        <!-- Available Colors -->
                                        <Grid Grid.Row="1" Visibility="{TemplateBinding ShowAvailableColors, Converter={StaticResource BooleanToVisibilityConverter}}">
                                            <Grid>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="Auto" />
                                                    <RowDefinition />
                                                </Grid.RowDefinitions>
                                                <TextBlock Text="{x:Static localiz:Strings.STR_Available_Colors_LABEL}" Padding="2" Margin="3" />
                                                <ListBox x:Name="PART_AvailableColors"
                                                         Grid.Row="1"
                                                         ItemsSource="{Binding AvailableColors, RelativeSource={RelativeSource TemplatedParent}}"
                                                         Style="{StaticResource ColorListStyle}" />
                                            </Grid>
                                        </Grid>

                                        <!-- Standard Colors-->
                                        <Grid Grid.Row="2" Visibility="{TemplateBinding ShowStandardColors, Converter={StaticResource BooleanToVisibilityConverter}}">
                                            <Grid>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="Auto" />
                                                    <RowDefinition Height="Auto" />
                                                </Grid.RowDefinitions>
                                                <TextBlock Text="{x:Static localiz:Strings.STR_Standard_Colors_LABEL}" Padding="2" Margin="3" />
                                                <ListBox x:Name="PART_StandardColors"
                                             Grid.Row="1"
                                             ItemsSource="{Binding StandardColors, RelativeSource={RelativeSource TemplatedParent}}"                                                      
                                             Style="{StaticResource ColorListStyle}" />
                                            </Grid>
                                        </Grid>

                                        <!-- Recent Colors-->
                                        <Grid Grid.Row="3" Margin="0,1,0,1" Visibility="{TemplateBinding ShowRecentColors, Converter={StaticResource BooleanToVisibilityConverter}}">
                                            <Grid>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="Auto" />
                                                    <RowDefinition Height="Auto" />
                                                </Grid.RowDefinitions>
                                                <TextBlock Text="{x:Static localiz:Strings.STR_Recent_Colors_LABEL}" Padding="2" Margin="0,1,0,1" />
                                                <ListBox x:Name="PART_RecentColors"
                                             Grid.Row="1"
                                             ItemsSource="{Binding RecentColors, RelativeSource={RelativeSource TemplatedParent}}"
                                             Style="{StaticResource ColorListStyle}" />
                                            </Grid>
                                        </Grid>
                                    </Grid>

                                    <!-- ColorCanvas -->
                                    <Grid x:Name="_colorCanvasHost" Visibility="Collapsed">
                                        <controls:ColorCanvas Background="Transparent"
                                                             BorderThickness="0"
                                                             UsingAlphaChannel="{Binding UsingAlphaChannel, RelativeSource={RelativeSource TemplatedParent}}"
                                                             SelectedColor="{Binding SelectedColor, RelativeSource={RelativeSource TemplatedParent}}" />
                                    </Grid>

                                    <Separator Grid.Row="1"
                                              HorizontalAlignment="Stretch"
                                              Margin="5,0,5,0"
                                              Visibility="{TemplateBinding ShowAdvancedButton, Converter={StaticResource BooleanToVisibilityConverter}}" />

                                    <!-- More Colors Button -->
                                    <Button x:Name="PART_ColorModeButton" Grid.Row="2" Margin="5" Visibility="{TemplateBinding ShowAdvancedButton, Converter={StaticResource BooleanToVisibilityConverter}}" />
                                </Grid>
                            </Border>
                        </Popup>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="DisplayColorAndName" Value="True">
                            <Setter TargetName="ColorOnly" Property="Visibility" Value="Collapsed" />
                            <Setter TargetName="ColorAndName" Property="Visibility" Value="Visible" />
                        </Trigger>

                        <Trigger Property="ColorMode" Value="ColorPalette">
                            <Setter TargetName="_colorPaletteHost" Property="Visibility" Value="Visible" />
                            <Setter TargetName="_colorCanvasHost" Property="Visibility" Value="Collapsed" />
                            <Setter TargetName="PART_ColorModeButton"
                             Property="Content"
                             Value="{Binding AdvancedButtonHeader, RelativeSource={RelativeSource TemplatedParent}}" />
                        </Trigger>

                        <Trigger Property="ColorMode" Value="ColorCanvas">
                            <Setter TargetName="_colorPaletteHost" Property="Visibility" Value="Collapsed" />
                            <Setter TargetName="_colorCanvasHost" Property="Visibility" Value="Visible" />
                            <Setter TargetName="PART_ColorModeButton"
                             Property="Content"
                             Value="{Binding StandardButtonHeader, RelativeSource={RelativeSource TemplatedParent}}" />
                        </Trigger>

                        <Trigger Property="ShowDropDownButton" Value="False">
                            <Setter Property="BorderThickness" Value="1" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="PART_ColorPickerToggleButton" Property="BorderBrush" Value="{DynamicResource {x:Static themes:ResourceKeys.ButtonFocusedOuterBorderKey}}" />
                        </Trigger>
                        <Trigger Property="IsOpen" Value="True">
                            <Setter TargetName="PART_ColorPickerToggleButton" Property="BorderBrush" Value="{DynamicResource {x:Static themes:ResourceKeys.ButtonPressedInnerBorderKey}}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>